layui.use(['table', 'element', 'form', 'layer'], function () {
    var element = layui.element;
    var table = layui.table;
    var $ = layui.$;
    var form = layui.form;
    var layer = layui.layer;


    table.render({
        elem: '#0-table'
        , url: '/emp/getDimission'
        , where: {
            status: "待处理",
            me: 0
        }
        , id: "dimission-0"
        , page: true
        , height: 512
        , limit: 10
        , cols: [[
            // {field: 'emp', title: '员工编号'}
            {field: 'empName', title: '员工'}
            , {field: 'time', title: '申请时间', width: 170}
            // , {field: 'department', title: '部门编号'}
            // ,{field:'area',title:'区域'}
            // ,{field:'nativePlace',title:'籍贯'}
            // ,{field:'state',title:'状态'}
            , {field: 'dName', title: '离职部门'}
            , {field: 'type', title: '类别'}
            , {field: 'reason', title: '申请原因', width: 160}
            , {field: 'status', title: '申请状态'}
            , {field: 'operation', title: '操作', fixed: 'right', width: 180, align: 'center', toolbar: '#barDemo'}
        ]]
    });

    table.render({
        elem: '#4-table'
        , url: '/emp/getDimission'
        , where: {
            status: 0,
            me: 1
        }
        , id: "dimission-4"
        , page: true
        , limit: 10
        , cols: [[
            // {field: 'emp', title: '员工编号'}
            {field: 'empName', title: '员工'}
            , {field: 'time', title: '申请时间', width: 170}
            // , {field: 'department', title: '部门编号'}
            // ,{field:'area',title:'区域'}
            // ,{field:'nativePlace',title:'籍贯'}
            // ,{field:'state',title:'状态'}
            , {field: 'dName', title: '离职部门'}
            , {field: 'type', title: '类别'}
            , {field: 'reason', title: '申请原因', width: 160}
            , {field: 'status', title: '申请状态'}
            // , {field: 'operation', title: '操作', fixed: 'right', width: 110, align: 'center', toolbar: '#bar1'}
        ]]
    });


    table.render({
        elem: '#1-table'
        , url: '/emp/getDimission'
        , where: {
            status: "已处理",
            me: 0
        }
        , id: "dimission-1"
        , page: true
        , limit: 10
        , cols: [[
            // {type: 'checkbox'}
            {field: 'empName', title: '员工'}
            , {field: 'time', title: '申请时间', width: 170}
            // , {field: 'department', title: '部门编号'}
            // ,{field:'area',title:'区域'}
            // ,{field:'nativePlace',title:'籍贯'}
            // ,{field:'state',title:'状态'}
            , {field: 'dName', title: '离职部门'}
            , {field: 'type', title: '类别'}
            , {field: 'reason', title: '申请原因', width: 160}
            , {field: 'status', title: '申请状态'}
            // , {field: 'operation', title: '操作', fixed: 'right', width: 110, align: 'center', toolbar: '#bar2'}
        ]]
    });


    element.on('tab(tab)', function (data) {
        var index = data.index;
        console.log(index);
        if (index == 0) {
            location.href = '/emp';
        } else if (index == 1) {
            location.href = '/emp/department';
        } else if (index == 2) {
            location.href = '/emp/entry';
        }
        return false;
    });

    var newDimission = $('#newDimission');
    var dimissionForm = $('#newDimissionForm');
    dimissionForm.hide();
    newDimission.click(function () {

        layer.open({
            title: "离职申请",
            type: 1,
            area: 'auto',
            maxWidth: 1000,
            content: $("#newDimissionForm")
        });
        dimissionForm.show();
        return false;
    });


    form.on('submit(newDimissionSubmit)', function (data) {
        $.post('/emp/newDimission', data.field, function (data) {
            if (!data.status) {
                layer.open({title: '警告', content: data.msg});
            } else {
                dimissionForm[0].reset();
                layer.close(layer.index);
                layer.open({
                    title: '成功',
                    content: "离职申请成功！"
                });
                table.reload('dimission-0');
                table.reload('dimission-4');
            }
        });
        return false;
    });


    table.on('tool(dimission-0)', function (obj) {
        var data = obj.data;
        var layEvent = obj.event;
        console.log(data);
        if (layEvent === 'agree') {
            layer.confirm('确认批准吗？批准后该员工即刻离职', {icon: 3, title: '确认'}, function (index) {
                agreeDimission(data.id);
                layer.close(index);
            })

        } else if (layEvent === 'refuse') {

            layer.confirm('确认否决吗？', {icon: 3, title: '确认'}, function (index) {
                refuseDimission(data.id);
                layer.close(index);
            })

        } else if (layEvent === 'del') {
            layer.confirm('确认撤销吗？', {icon: 3, title: '确认'}, function (index) {
                deleteDimission(data.id);
                layer.close(index);
            })
        }
    });

    function agreeDimission(id) {
        $.post('/emp/agreeDimission', {id: id}, function (data) {
            if (!data.status) {
                layer.open({title: '警告', content: data.msg});
            } else {
                layer.open({title: '成功', content: '已批准'});
                table.reload('dimission-0');
                table.reload('dimission-4');
                table.reload('dimission-1');
            }
        });
    }

    function refuseDimission(id) {
        $.post('/emp/refuseDimission', {id: id}, function (data) {
            if (!data.status) {
                layer.open({title: '警告', content: data.msg});
            } else {
                layer.open({title: '成功', content: '已拒绝'});
                table.reload('dimission-0');
                table.reload('dimission-4');
                table.reload('dimission-1');
            }
        });
    }

    function deleteDimission(id) {
        $.post('/emp/deleteDimission', {id: id}, function (data) {
            if (!data.status) {
                layer.open({title: '警告', content: data.msg});
            } else {
                layer.open({title: '成功', content: '已撤销'});
                table.reload('dimission-0');
                table.reload('dimission-4');
                table.reload('dimission-1');
            }
        });
    }


    var newFire = $('#newFire');
    var fireForm = $('#newFireForm');
    fireForm.hide();
    newFire.click(function () {
        layer.open({
            title: "离职申请",
            type: 1,
            area: 'auto',
            maxWidth: 1000,
            content: $("#newFireForm")
        });
        fireForm.show();
        return false;
    });

    form.on('submit(newFireSubmit)', function (data) {
        $.post('/emp/newFire', data.field, function (data) {
            if (!data.status) {
                layer.open({title: '警告', content: data.msg});
            } else {
                dimissionForm[0].reset();
                layer.closeAll();
                layer.open({
                    title: '成功',
                    content: "新增开除成功！"
                });
                table.reload('dimission-0');
                table.reload('dimission-4');
            }
        });
        return false;
    })

});

